#!/bin/sh

exePath=`dirname "${0}"`
pkgPath="${exePath}"/../Resources/SqueezeInstall.mpkg

timeStart=0`date +"%Y%m%d%H%M%S"`

sysVersion=`system_profiler -detailLevel mini | grep 'System Version: ' | cut -d: -f2 | cut -d'(' -f1 | cut -d' ' -f5 | rev | cut -d. -f2- | rev`

if [ "${sysVersion}" = "10.4" ]; then
	/Applications/Utilities/Installer.app/Contents/MacOS/Installer "${pkgPath}"
else
	/usr/bin/open "${pkgPath}"


	## Wait for installer to open
	sleep 2

	## we can only deal with one installer running. Otherwise just exit.
	result=`ps -ax | grep Installer.app/Contents/MacOS/Installer | grep -v grep | wc -l`
	let installerCount="$result"
	if [ "$installerCount" = 0 -o "$installerCount" -gt 1 ]; then
		exit 0
	fi

	## Wait for installer to finish before trying to start Squeeze
	done=0
	while [ $done == 0 ]; do
 		mystr=`ps -ax | grep Installer.app/Contents/MacOS/Installer | grep -v grep`
		if [ "$mystr" == "" ]; then
 			done=1
		fi
		sleep 1
	done
fi

sqzPlist="/Library/Preferences/com.sorensonmedia.Squeeze 6"
#sqzPlist="/Library/Preferences/com.sorensonmedia.Squeeze Beta"
if [ -e "${sqzPlist}".plist ]; then
	###timePlist=`stat -t "%Y%m%d%H%M%S" -f "%Sm" "${sqzPlist}".plist`
	timePlist=0`defaults read "${sqzPlist}" TimeInstalled 2>>/dev/null`
	if [ "${timeStart}" -le "${timePlist}" ]; then
		sqzPath=`defaults read "${sqzPlist}" Path 2>>/dev/null`/Squeeze.app
		if [ -e "$sqzPath" ]; then
			open "${sqzPath}"
		fi
	fi
fi

exit 0
